#!/usr/bin/env pypy

import unittest, os, math

import playlist_module

import lib

class TestLib(unittest.TestCase):

	filename = "foobar.test.gz"

	def setUp(self):
		self.data = {
			"Foo": "Bar"
		}

	@classmethod
	def tearDownClass(cls):
		if os.path.exists(cls.filename):
			os.remove(cls.filename)

	def test_sciRound_2_6(self):
		self.assertEqual(lib.sciRound(2.6), 3)

	def test_sciRound_2_5(self):
		self.assertEqual(lib.sciRound(2.5), 2)

	def test_sciRound_2_4(self):
		self.assertEqual(lib.sciRound(2.4), 2)

	def test_sciRound_3_6(self):
		self.assertEqual(lib.sciRound(3.6), 4)

	def test_sciRound_3_5(self):
		self.assertEqual(lib.sciRound(3.5), 4)

	def test_sciRound_3_4(self):
		self.assertEqual(lib.sciRound(3.4), 3)

	def test_sciRound__5(self):
		self.assertEqual(lib.sciRound(.5), 0)

	def test_1_save(self):
		lib.save(self.data, TestLib.filename)

	def test_2_load(self):
		data2 = lib.load(TestLib.filename)

		self.assertEqual(data2, self.data)

	def test_calcTPD(self):
		lengths = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10]

		count = len(lengths)
		hTotal = 0
		aTotal = 0
        
		for length in lengths:
			hTotal = hTotal + (1.0 / length)
			aTotal = aTotal + (1.0 * length)
        
		hMean = count / hTotal
        
		aMean = aTotal / count
        
		gMean = math.sqrt(aMean * hMean)
        
		TPD = (24 * 60 * 60) / gMean

		self.assertEqual(lib.calcTPD(lengths), TPD)

if __name__ == '__main__':
	unittest.main()

